<?php

/*
info: 发送邮件类
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

if (defined('EMAIL_CLASS'))
{
	return;
}
define('EMAIL_CLASS', TRUE);

class Email
{
	public $errorMsg = array();
	private $smtpInfo = array('localhost'=>'', 'username'=>'', 'password'=>'', 'nickname'=>'', 'smtphost'=>'', 'from'=>'', 'verify'=>TRUE, 'port'=>25);
	private $lineBreak = "\r\n";

//构造函数
	public function Email($arySmtp = array())
	{
		if (!empty($arySmtp))
		{
			$this->SetSmtp($arySmtp);
		}
	}

//清除错误信息
	public function ClearErrorMsg()
	{
		$this->errorMsg = array();
	}

//发送邮件
	public function Send($mixedTo, $strSubject = '无标题', $strBody = '', $intIsHtml = 0)
	{
		if (empty($this->smtpInfo['smtphost']))
		{
			$this->SetDefault();
		}

		if (is_array($mixedTo))
		{
			$aryTo = $mixedTo;
			$strTo = implode(',', $aryTo);
		}
		else
		{
			$strTo = $mixedTo;
			$aryTo = explode(',', $strTo);
		}
		if (!empty($strBody))
		{
			$strBody = chunk_split(base64_encode(str_replace("\r\n.", "\r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $strBody)))))));
		}

		$tempsmtp = array();
		//1、HELO，期待返回220或者250
		$tempsmtp[] = array("HELO ".$this->smtpInfo['localhost'].$this->lineBreak, "220,250", "[HELO error] ");
		if ($this->smtpInfo['verify'])
		{
			//2、发送Auth Login，期待返回334
			$tempsmtp[] = array("AUTH LOGIN".$this->lineBreak, "334", "[AUTH error] ");
			//3、发送经过Base64编码的用户名，期待返回334
			$tempsmtp[] = array(base64_encode($this->smtpInfo['username']).$this->lineBreak, "334", "[AUTHENTIFICATION user error] ");
			//4、发送经过Base64编码的密码，期待返回235
			$tempsmtp[] = array(base64_encode($this->smtpInfo['password']).$this->lineBreak, "235", "[AUTHENTIFICATION pass error] ");
		}
		//5、发送Mail From，期待返回250
		$tempsmtp[] = array("MAIL FROM: <".$this->smtpInfo['from'].">".$this->lineBreak, "250", "[MAIL FROM error] ");
		//6、发送Rcpt To。期待返回250
		foreach ($aryTo as $tempto)
		{
			$tempsmtp[] = array("RCPT TO: <".trim($tempto).">".$this->lineBreak, "250", "[RCPT TO error] ");
		}
		//7、发送DATA，期待返回354
		$tempsmtp[] = array("DATA".$this->lineBreak, "354", "[DATA error] ");
		//8.0、发送From
		$tempsmtp[] = array("From: \"".$this->smtpInfo['nickname']."\" <".$this->smtpInfo['from'].'>'.$this->lineBreak, "", "");
		//8.1、发送To
		$tempsmtp[] = array("To: ".$strTo.$this->lineBreak, "", "");
		//8.2、发送标题
		$tempsmtp[] = array("Subject: ".$strSubject.$this->lineBreak, "", "");
		//8.3、发送其他Header内容
		$temptype = ($intIsHtml==1) ? "text/html" : "text/plain";
		$tempsmtp[] = array("Content-Type: {$temptype}; charset=\"utf-8\"".$this->lineBreak, "", "");
		$tempsmtp[] = array("Content-Transfer-Encoding: base64".$this->lineBreak, "", "");
		//8.4、发送一个空行，结束Header发送
		$tempsmtp[] = array($this->lineBreak, "", "");
		//8.5、发送信件主体
		$tempsmtp[] = array($strBody.$this->lineBreak, "", "");
		//9、发送“.”表示信件结束，期待返回250
		$tempsmtp[] = array(".".$this->lineBreak, "250", "[DATA end error] ");
		//10、发送Quit，退出，期待返回221
		$tempsmtp[] = array("QUIT".$this->lineBreak, "221", "[QUIT error] ");

		//打开smtp服务器端口
		$fp = @fsockopen($this->smtpInfo['smtphost'], $this->smtpInfo['port']);
		if (!$fp)
		{
			$this->errorMsg[] = '[Error] Cannot conect to server.';
		}
		else
		{
			while ($tempresult = @fgets($fp, 1024))
			{
				if (substr($tempresult, 3, 1)==" ")
				{
					break;
				}
			}

			//发送smtp数组中的命令/数据
			foreach ($tempsmtp as $tempsmtpone)
			{
				//发送信息
				@fputs($fp, $tempsmtpone[0]);
				//需要接收服务器返回信息时处理
				if ($tempsmtpone[1]!="")
				{
					//接收信息
					while ($tempresult = @fgets($fp, 1024))
					{
						if (substr($tempresult, 3, 1)==" ")
						{
							break;
						}
					}
					if (!strstr($tempsmtpone[1], substr($tempresult, 0, 3)))
					{
						$this->errorMsg[] = $tempsmtpone[2].$tempresult;
						break;
					}
				}
			}
		}
		//关闭连接
		@fclose($fp);
	}

//设置SMTP服务器信息
	public function SetSmtp($arySmtp)
	{
		if (is_array($arySmtp) && !empty($arySmtp))
		{
			foreach ($this->smtpInfo as $key => $value)
			{
				if (isset($arySmtp[$key]))
				{
					$this->smtpInfo[$key] = $arySmtp[$key];
				}
			}
		}
	}


//----------------------------------------------------------------------------


//设置默认帐号
	private function SetDefault()
	{
		global $webconfig;
		$this->smtpInfo['localhost'] = $webconfig['mail_helo'];
		$this->smtpInfo['username'] = $webconfig['mail_auth_username'];
		$this->smtpInfo['password'] = $webconfig['mail_auth_password'];
		$this->smtpInfo['nickname'] = $webconfig['mail_from_name'];
		$this->smtpInfo['smtphost'] = $webconfig['mail_server'];
		$this->smtpInfo['from'] = $webconfig['mail_from'];
		if (isset($webconfig['mail_auth']))
		{
			$this->smtpInfo['verify'] = $webconfig['mail_auth'];
		}
		if (!empty($webconfig['mail_port']))
		{
			$this->smtpInfo['port'] = $webconfig['mail_port'];
		}
	}

}	//end class
